package com.imcrl.component.page
{
	import com.imcrl.core.IDatable;
	import com.imcrl.core.IRenderable;
	import com.imcrl.manager.RenderManager;
	
	import starling.events.Event;

    /**
     * 分页数据功能;
     * @author crl
     *
     */	
    public class Page extends AbstractPage implements IPageable ,IDatable
    {
        protected var _data:*;
        public function Page(pageSize:uint=5)
        {
           _size= pageSize;
        }

        override public function set pageSize(value:uint):void
        {
            super.pageSize=value;
            this.invalidate();
        }
		
		override public function get dataLength():int{
			return _data.length;
		}
		

        override public function set current(value:uint):void
        {
            super.current=value;
            this.invalidate();
        }

		override public function getPageData(pageIndex:int):Array{
			var result:Array=[];
			var start:uint;
			var end:uint;
			
			start=pageIndex*pageSize;
			if(pageIndex==_total-1){
				end=dataLength;
			}else{
				end=start+_size;
			}
			
			var value:*;
			for(var i:uint = start ; i < end ; i++){
				value=_data[i];
				if(value)result.push(value);
			}
			return result;
		}


        public function get data():*{
            return _data
        }
        public function set data(value:*):void{
			_data=value;
            if(_data==null){
				_data=[];
            }

            this.invalidate();
        }

        public function invalidate():void{

            this._total=Math.ceil(dataLength/_size);

            if(_total<1){
				_current=0;
                this.dispatchEvent(new Event(Event.COMPLETE));
                return;
            }

            if(_current>_total){
                _current=Math.max(0,_total-1);
            }

            this.dispatchEvent(new Event(Event.COMPLETE));
        }
    }
}

